import $service from '../../../../../service.js';
var that;
var doctor_id;
var obj = {};
var hospital_id="1"
Page({
    data: {
        isshow: false,
        isClassList: 1,
        nextClassList: [],//一级列表
        checklistList: [],//二级列表
        opinionList: [],//已选中检验项目
        dataId2: '',
        checklist_abstract:'', //摘要
        checklist_inspection_result : '',//有关检验结果
        bz:'',//备注
        inquiryMap:''//诊断info
    },
    onLoad(e) {
        that = this.data;
        obj = $service.strToJson(e.obj)
        that.opinionList.push(obj.checklistList)
        this.setData({
            inquiryMap : obj.inquiryMap,
            opinionList: that.opinionList
        })
    },
    onShow() {
        that = this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if(!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)){
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        this.doctorSide_personal_inquiry_getUtilChecklistClass()
    },
    //检验检查数据
    doctorSide_personal_inquiry_getUtilChecklistClass: function() {
        var jsonData = {
            "hospital_id": obj.hospital_id,
            "checklist_class_id": obj.checklist_class_id
        }
        var jsonAjax = {
            "url": "doctorSide_personal_inquiry_getUtilChecklistClass.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_getUtilChecklistClass_back);
    },
    //回调
    doctorSide_personal_inquiry_getUtilChecklistClass_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var checklistClassList = data.checklistClassList; //大分类
        for (var i = 0; i < checklistClassList.length; i++) {
            checklistClassList[i].isshow = false;
        }
        if (data.isClassList == 1) { //是否返回分类list  0否 1是
            that.nextClassList = data.nextClassList; //下级分类列表
            for (var i = 0; i < that.nextClassList.length; i++) {
                that.nextClassList[i].istwoshow = false;
            }
        } else {
            if (!$service.isUndefinedAndEmpty(data.checklistList)) {
                that.checklistList = data.checklistList; //下级分类列表
            }
            if (!$service.isUndefinedAndEmpty(that.dataId2)) {
                that.checklistList = data.checklistList; //下级分类列表
            }
        }
        if (!$service.isUndefinedAndEmpty(that.dataId2)) { //二级列表点击展开/隐藏
            this.setData({
                checklistList: that.checklistList,
                isClassList: data.isClassList
            })
        } else {
            this.setData({
                nextClassList: that.nextClassList,
                checklistList: that.checklistList,
                isClassList: data.isClassList
            })
        }
    },
    tapisshow(e) {
        this.data.isshow = !this.data.isshow
        this.setData({
            isshow: this.data.isshow
        })
    },
    //列表点击事件
    checklistClasstap(e) {
        console.log(e)
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        that.dataId2 = that.nextClassList[index].checklist_class_id;
        obj.checklist_class_id = that.dataId2;
        for (var i = 0; i < that.nextClassList.length; i++) {
            if (that.nextClassList[i].checklist_class_id == that.dataId2) {
                that.nextClassList[i].istwoshow = !that.nextClassList[i].istwoshow;
            } else {
                that.nextClassList[i].istwoshow = false;
            }

        }
        this.setData({
            dataId2: that.dataId2,
            nextClassList: that.nextClassList,
            checklistList: []
        })
        this.doctorSide_personal_inquiry_getUtilChecklistClass()

    },
    //添加至检验检查项目
    addOpinion(e) {
        console.log(e)
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        var type = e.currentTarget.dataset.type;
        if (num == 1) { //检验检查
            if (that.opinionList.length > 4) {
                $service.showToast('最多五个');
                return;
            }
            for (var i = 0; i < that.opinionList.length; i++) {
                if (that.checklistList[index].checklist_id == that.opinionList[i].checklist_id) {
                    $service.showToast('请不要重复添加');
                    return;
                }
            }
            that.opinionList.push(that.checklistList[index])
        }
        this.setData({
            opinionList: that.opinionList
        })
    },
    //删除检验项目
    delopinionList(e) {
        if(that.opinionList.length > 1){
            var index = e.currentTarget.dataset.index;
            var num = e.currentTarget.dataset.num;
            that.opinionList.splice(index, 1)
            this.setData({
                opinionList: that.opinionList
            })
        }else{
            $service.showToast('至少保留一个');
            return
        } 
       
    },
    //保存、
    addchecklist() {
        var jsonData = {
            "checklistType": "3",
            "checklist_type": "4",
            "checklist_id": that.opinionList[0].checklist_id,
            "log_id": obj.inquiryMap.log_id,
            "join_type": obj.inquiryMap.join_type,
            "join_id": obj.inquiryMap.join_id,
            "inquiry_patient_id": obj.inquiryMap.inquiry_patient_id,
            "doctor_id": doctor_id,
            "hospital_id": hospital_id,
            "checklist_name": that.opinionList[0].checklist_name,
            "checklist_abstract": that.checklist_abstract, //摘要
            "checklist_inspection_result": that.checklist_inspection_result, //有关检验结果
            "bz": that.bz, //备注
            "detailsList": $service.jsonToStr(that.opinionList)
        }
        console.log("//普通检查===" + $service.jsonToStr(jsonData))
        for (var i = 0; i < obj.checklistListAll.length; i++) {
            if (obj.checklistListAll[i].checklist_name == jsonData.checklist_name) { //判断是否重复
                $service.showToast("添加申请单重复!");
                return false;
            }
        }
        var jsonAjax = {
            "url": "doctorSide_personal_inquiry_addInquiryLogCheckList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax,function(jsonObj) {
           var selToID='';
           if(obj.inquiryMap.join_type == 1){
                selToID = "U" + obj.inquiryMap.join_id
           }else{
                 selToID = "S" + obj.inquiryMap.join_id
           }
           var sendSystemMessageMsgText = '医生已开具检验检查单'
           wx.txTic.sendCustomMessage(selToID,6)
           wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
            wx.showToast({"title":'保存成功',"icon":"none",duration: 4000});
            setTimeout(function(){
                wx.navigateBack({
                    delta:1
                })
            },1500);
       });
    },
    //监听输入框
    inputChange(e){
        var index = e.currentTarget.dataset.index;
        if(index == 1){//摘要
            that.checklist_abstract = e.detail.value
        }else if(index == 2){
             that.checklist_inspection_result = e.detail.value
        }else if(index == 3){
             that.bz = e.detail.value
        }
        this.setData({
            checklist_abstract : that.checklist_abstract,
            checklist_inspection_result:  that.checklist_inspection_result,
            bz:that.bz
        })
    }
})
